<template>
  <div>
    <Row class="search-info-style">
          <Col span="6" v-if="companyGrade < 3">
              <span>区县：</span>
              <Select v-model="searchInfo.areaid" filterable transfer="" clearable @on-change="getMailList2(4,searchInfo.areaid)" style="width:200px">
                <Option :value="item.code" v-for="item in mailList1" v-bind:key="item.code">{{item.name}}</Option>
              </Select>
          </Col>
          <Col span="6" v-if="companyGrade < 4">
              <span>街道：</span>
              <Select v-model="searchInfo.streetId" filterable transfer="" clearable @on-change="getMailList2(5, searchInfo.streetId)" style="width:200px">
                <Option :value="item.code" v-for="item in mailList2" v-bind:key="item.code">{{item.name}}</Option>
              </Select>
          </Col>
          <Col span="6" v-if="companyGrade < 5">
              <span>社区：</span>
              <Select v-model="searchInfo.communityId" filterable transfer="" clearable style="width:200px">
                <Option :value="item.code" v-for="item in mailList3" v-bind:key="item.code">{{item.name}}</Option>
              </Select>
          </Col>
          <Col span="2" style="float:right" pull="1">
              <Button class="new-btn-sty" @click="findtime()"><Icon type="ios-search" />搜索</Button>
          </Col>
      </Row>
      <Table border :columns="columns1" :data="datalist"></Table>
      <Row v-if="loadData == true">
        <Col class="demo-spin-col" span="8" offset="8">
          <Spin fix>
            <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
            <div>数据正在加载</div>
          </Spin>
        </Col>
      </Row>
      <Page class="page_style" :total="searchInfo.totalnum" :current="searchInfo.currentPageNo"
              :page-size="searchInfo.currentPageSize" @on-change="changePageNo" show-total  show-sizer @on-page-size-change="size"/>
      <Modal v-model="modalEdit" title="详细信息" width="700" cancel-text="关闭"
        @on-ok="ok111(paramData2)" @on-cancel="modalEdit=false" ok-text="确定">
        <Form :model="paramData2" >
          <Row class="item1" style="height:auto;line-height:auto">
            <Col span="5" style="text-align: right" class="">牌子名称:</Col>
            <Col span="12" offset="1">
              <Input v-model="paramData2.brand_name"></Input>
            </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">牌子类别:</Col>
            <Col span="12" offset="1">
            <!-- 1.工作服务类/2.评比表彰类/3.创建示范类/0.其他 -->
                <Select v-model="paramData2.category">
                  <Option value="0">其他</Option>
                  <Option value="1">工作服务类</Option>
                  <Option value="2">评比表彰类</Option>
                  <Option value="3">创建示范类</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">位置:</Col>
            <Col span="12" offset="1">
                <Select v-model="paramData2.room">
                  <Option value="0">室内</Option>
                  <Option value="1">室外</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1">
            <Col span="5" style="text-align: right" class="">牌子级别:</Col>
            <Col span="12" offset="1">
                <Select v-model="paramData2.level">
                  <Option value="1">市级</Option>
                  <Option value="2">区/县</Option>
                  <Option value="3">街道</Option>
                </Select>
              </Col>
          </Row>
          <Row class="item1" style="height:180px">
            <Col span="5" style="text-align: right" class="">牌子照片:</Col>
            <Col span="12" offset="1">
                <img :src="showphoto" class="hd-img-responsive person-info-header"/>
                <Icon @click="updatePhoto" type="md-camera" size="40" style="position:absolute;z-index:100;left:330px;top: 150px"/>
              </Col>
          </Row>
        </Form>
      </Modal>
      <Modal v-model="up_pic_mol" title="图片上传" :closable="false">
      <Upload multiple action="http://10.40.20.4:8081/api/uc/mzsc/uploadService/uploadPic" :before-upload="beforeUpload">
        <Button icon="ios-cloud-upload-outline">上传图片</Button>
        <span style="margin-left: 250px">请上传小于2M的图片</span>
      </Upload>
      <img :src="temp_photo" style="width: 100%"/>
      </Modal>
      <Modal v-model="modalEdit4" title="详细信息" width="50%" cancel-text="关闭" @on-cancel="modalEdit4=false" ok-text="确定" style="text-align:center">
      <!-- 由于当时想当然地以为，src 就是绑定一个变量，所以就设置了一个默认变量，这是出错的过程 -->
      <div v-for="(item, index) in flowers" :key="index" width="20%">
        <img :src="item.brand_photo" preview :title="item" width="350px" height="260px" style="color: #457eca; font-size: 14px;">
        <p class="word-v-middle" style="font-size: 25px;">牌子名称: {{item.brand_name}}</p></img>
        <Divider style="size: 15px" />
      </div>
    </Modal>
  </div>
</template>
<script>
import nophoto from '@/assets/images/nophoto.jpeg'
import { findBrandStatisticsList, getPhotoAll, getOfficeList } from '@/api/eventlist'
import { createUpLoadFiles, getSign } from '@/api/common'
export default {
  data () {
    return {
      mailList1: [],
      mailList2: [],
      mailList3: [],
      loadData: true,
      temp_photo: '',
      userInfo: { photo: nophoto },
      showphoto: '',
      upPicData: {
        fileName: '',
        filePath: ''
      },
      fileName: '',
      modalEdit4: false,
      flowers: [],
      modalEdit: false,
      PhotoModal: false,
      up_pic_mol: false,
      paramData2: {},
      searchInfo: {
        grade: 0,
        totalnum: 0,
        currentPageNo: 1,
        currentPageSize: 10
      },
      param: {},
      mailList: [[], [], []],
      datalist: [],
      columns1: [
        { title: '序号', type: 'index', key: 'index', width: 100, align: 'center' },
        { title: '社区', key: 'name', width: 350, align: 'center' },
        { title: '应挂', key: 'allnum', width: 250, align: 'center' },
        { title: '实挂', key: 'num', width: 250, align: 'center' },
        // { title: '创建时间', key: 'create_time', width: 200, align: 'center' },
        // { title: '位置', key: 'room_name', width: 200, align: 'center' },
        // { title: '级别', key: 'level_name', width: 200, align: 'center' },
        // { title: '状态', key: 'state_name', width: 100, align: 'center' },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          minWidth: 150,
          render: (h, params) => {
            let temp = []
            temp.push(
              h('Button', {
                props: { type: 'primary', size: 'small' },
                style: { marginRight: '5px', backgroundColor: '#598ED1', borderColor: '#598ED1' },
                on: {
                  click: () => {
                    this.modalEdit4 = true
                    this.imgALLData(params.row)
                  }
                }
              }, '详情')
            )
            return h('div', temp)
          }
        }
      ]
    }
  },
  methods: {
    init (param) {
      param.pageNo = this.searchInfo.currentPageNo
      param.pageSize = this.searchInfo.currentPageSize
      findBrandStatisticsList(param).then(res => {
        if (res.data.errcode === 0) {
          this.datalist = res.data.data.results
          this.searchInfo.totalnum = res.data.data.totalRecord
          this.datalist.forEach(function (item1, index) {
            if (item1.state === '0') {
              item1.state_name = '在挂'
            } else if (item1.state === '1') {
              item1.state_name = '已撤'
            }
            if (item1.subsidiary_name) {
              item1.brand_name = item1.subsidiary_name
            }
          })
          this.loadData = false
        } else {
          this.loadData = true
          this.$Notice.error({ title: '获取发起列表失败', desc: res.data.errmsg })
        }
      })
    },
    findtime () {
      if (this.searchInfo.areaid) {
        this.param.code = this.searchInfo.areaid
      }
      if (this.searchInfo.streetId) {
        this.param.code = this.searchInfo.streetId
      }
      // this.param.code = this.searchInfo.areaid
      // this.param.code = this.searchInfo.streetId
      this.param.communityId = this.searchInfo.communityId
      // this.param.category = this.searchInfo.category
      // this.param.room = this.searchInfo.room
      console.log(this.param, '55555555555')
      this.init(this.param)
    },
    // 社区统计下拉搜索框的级联查询
    getMailList (grade, code) {
      // this.searchInfo['grade' + (index + 1)] = 0
      getOfficeList({ grade: grade, code: code }).then(res => {
        if (res.data.errcode === 0) {
          this.mailList1 = res.data.data
          // this.mailList[index].unshift({ id: 0, title: '全部' })
          // this.$forceUpdate()
        } else {
          this.$Notice.error({ title: '获取信息失败', desc: '查询区域列表信息失败' })
        }
      })
    },
    getMailList2 (grade, code) {
      // this.searchInfo['grade' + (index + 1)] = 0
      getOfficeList({ code: code }).then(res => {
        if (res.data.errcode === 0) {
          if (grade === 4) {
            this.mailList2 = res.data.data
          } else if (grade === 5) {
            this.mailList3 = res.data.data
          }
          // this.mailList[index].unshift({ id: 0, title: '全部' })
          // this.$forceUpdate()
        } else {
          this.$Notice.error({ title: '获取信息失败', desc: '查询区域列表信息失败' })
        }
      })
    },
    add (item) {
      this.modalEdit = true
      this.paramData2.id = item.id || null
      this.paramData2.brand_name = item.brand_name || ''
      this.paramData2.category = item.category || ''
      this.paramData2.room = item.room || ''
      this.paramData2.level = item.level || ''
      this.showphoto = item.brand_photo || ''
    },
    // editNotice () {
    //   this.modalEdit = true
    // },
    size (pageSize) {
      this.searchInfo.currentPageSize = pageSize
      this.changePageNo()
    },
    changePageNo (pageNo) {
      console.log(pageNo)
      this.searchInfo.currentPageNo = pageNo
      this.init(this.param)
    },
    updatePhoto () {
      this.up_pic_mol = true
      this.PhotoModal = false
    },
    beforeUpload (file) {
      this.upPicData.fileName = this.upPicData.fileName + '.' + file.type.substring(6)
      let that = this
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = () => {
        let index = reader.result.indexOf('base64,') + 7
        that.upPicData.file = reader.result.substring(index)
        that.upPic(that.upPicData)
      }
      return false
    },
    ok111 (param) {
      param.brand_photo = this.showphoto
      console.log(param, '11111')
      saveUnqualifiedBrand(param).then(res => {
        if (res.data.errcode === 0) {
          this.init(this.param)
          this.$Notice.success({ title: '成功', desc: '' })
        } else {
          this.$Notice.error({ title: '失败', desc: res.data.errmsg })
        }
      })
      this.init(this.param)
    },
    upPic (obj) {
      var time = new Date().getTime()
      this.timeStep = time.toString()
      let param = {
        filePath: obj.filePath,
        fileName: obj.fileName,
        timestamp: this.timeStep
      }
      getSign(param).then(res => {
        if (res.data.errcode === 0) {
          createUpLoadFiles(res.data.data.docking_url, { sign: res.data.data.sign, timestamp: this.timeStep, filePath: obj.filePath, fileName: obj.fileName, file: obj.file }).then(res => {
            if (res.data.errcode === 0) {
              let imgurl = res.data.data
              this.temp_photo = imgurl
              this.showphoto = imgurl
              this.userInfo.photo = imgurl
            } else {
              this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
            }
          })
        } else {
          this.$Notice.error({ title: '上传失败', desc: res.data.data.msg })
        }
      })
    },
    imgALLData (item) {
      getPhotoAll(item).then(res => {
        console.log(res, '9999999')
        if (res.data.errcode === 0) {
          this.flowers = res.data.data
          console.log(this.flowers, 'aaaaa')
        } else {
          this.$Notice.error({ title: '获取信息失败', desc: res.data.errmsg })
        }
      })
    }
  },
  computed: {
    companyGrade () {
      return this.$store.state.user.severUserInfo.company_grade
    },
    companyCode () {
      return this.$store.state.user.severUserInfo.company_code
    }
  },
  mounted () {
    this.init(this.param)
    // this.getMailList(2, null)
    if (this.companyGrade < 3) {
      this.getMailList(2, null)
    } else if (this.companyGrade < 4) {
      this.getMailList2(4, this.companyCode)
    } else if (this.companyGrade < 5) {
      this.getMailList2(5, this.companyCode)
    }
  }
}
</script>
<style>
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .search-info-style {
    margin: 10px;
  }
  .item1{
    height: 42px;
    line-height: 40px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
</style>
